<%@page import="java.io.IOException,java.util.*,java.util.Map.Entry" %>
<%@page import="javax.servlet.*" %>
<%@page import="com.google.appengine.api.datastore.*,com.google.appengine.api.users.*" %>
<%@page import="com.padgett.resourcemanagement.data.ClientDAO,com.padgett.resourcemanagement.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML>
<html>
<head>
<title>Performance Report</title>
<%@include file="/WEB-INF/css.jsp" %>
<%@include file="/WEB-INF/javascript.jsp" %>
<style>
.ClientId{
float:left;
width:20%;
font:normal 9px verdana,times,serif;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.ClientNameHeader{
float:left;
width:45%;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.PlannedBillingHeader{
float:left;
width:24%;
text-align:center;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.ActualBillingHeader{
float:left;
width:24%;
text-align:center;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.ClientName{
float:left;
width:45%;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.PlannedBilling{
float:left;
width:24%;
text-align:right;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.ActualBilling{
float:left;
width:24%;
text-align:right;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.TotalLabel{
float:left;
width:45%;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.TotalPlannedBilling{
float:left;
width:24%;
text-align:right;
border:1px solid black;
padding:5px 5px 5px 5px;
}
.TotalActualBilling{
float:left;
width:24%;
text-align:right;
border:1px solid black;
padding:5px 5px 5px 5px;
}
</style>
<script>
$(document).ready(function(){
});
</script>
</head>
<body>
<div id="ReportContainer">
<%
GregorianCalendar today=new GregorianCalendar();
today.setTimeInMillis(System.currentTimeMillis());
List<Entity> services=ClientDAO.getAllServicesDuedByMonth(today.get(GregorianCalendar.MONTH));
// Build service map using client ID
Map<Key,List<Entity>> clientServiceMap=new HashMap<Key,List<Entity>>();
for(Entity service:services){
	Key serviceClientKey=(Key)service.getProperty("Client");
	if(clientServiceMap.containsKey(serviceClientKey)){
		List<Entity> clientServices=clientServiceMap.get(serviceClientKey);
		clientServices.add(service);
	}else{
		List<Entity> clientServices=new ArrayList<Entity>();
		clientServices.add(service);
		clientServiceMap.put(serviceClientKey, clientServices);
	}
}
Map<Key,Entity> clientMap=ClientDAO.getClientsByKeys(new ArrayList<Key>(clientServiceMap.keySet()));
List<ClientNameSortable> sortedClientList=new ArrayList<ClientNameSortable>();
Iterator<Entry<Key, Entity>> clientIterator=clientMap.entrySet().iterator();
while(clientIterator.hasNext()){
	Map.Entry<Key,Entity> entry=(Map.Entry<Key,Entity>)clientIterator.next();
	sortedClientList.add(new ClientNameSortable(KeyFactory.keyToString(entry.getKey()),(String)entry.getValue().getProperty("Name")));
}
// Sort client list by name
Collections.sort(sortedClientList);
try{
	StringBuilder output=new StringBuilder();
	output.append("Number of clients:"+sortedClientList.size()+"<br/>Number of Services:"+services.size()+"<br/><br/>");
	double totalPlannedBilling=0.0;
	double totalActualBilling=0.0;
	double plannedBilling=0.0;
	double actualBilling=0.0;
	output.append("<div style='float:left;width:97%;'><div class='ClientNameHeader'>Client</div><div class='PlannedBillingHeader'>Planned Billing</div><div class='ActualBillingHeader'>Actual Billing</div></div>");
	for(ClientNameSortable sortedClient:sortedClientList){
		plannedBilling=0.0;
		actualBilling=0.0;
		// For each client name, use key to get service
		List<Entity> clientServices=clientServiceMap.get(KeyFactory.stringToKey(sortedClient.getClientId()));
		if(clientServices!=null&&!clientServices.isEmpty()){
			for(Entity service:clientServices){
				boolean billed=(Boolean)(service.getProperty("Billed")!=null?service.getProperty("Billed"):Boolean.FALSE);
				double amount=(service.getProperty("Fee")!=null?(Double)service.getProperty("Fee"):0.0);
				plannedBilling+=amount;
				totalPlannedBilling+=plannedBilling;
				if(billed){
					actualBilling+=amount;
					totalActualBilling+=actualBilling;
				}
				//output.append("<div style='float:left;width:97%;'><div class='TotalLabel'>"+service.getProperty("Type")+"</div><div class='TotalPlannedBilling'>"+billed+"</div><div class='TotalActualBilling'>"+amount+"</div></div>");
			}
		}
		output.append("<div style='float:left;width:97%;'><div class='ClientName'>"+sortedClient.getName()+"</div><div class='PlannedBilling'>"+NumberUtil.formatCurrency(plannedBilling)+"</div><div class='ActualBilling'>"+NumberUtil.formatCurrency(actualBilling)+"</div></div>");
	}
	output.append("<div style='float:left;width:97%;'><div class='TotalLabel'>Total</div><div class='TotalPlannedBilling'>"+NumberUtil.formatCurrency(totalPlannedBilling)+"</div><div class='TotalActualBilling'>"+NumberUtil.formatCurrency(totalActualBilling)+"</div></div>");
	out.print(output.toString());
}catch(Exception e){
	e.printStackTrace();
}
%>
</div>
</body>
</html>